package com.uzai.console.common.utils;

import com.uzai.console.constant.CommonConstant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Stream;

public final class CommonUtil {
    private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);
    private static final SimpleDateFormat format = new SimpleDateFormat("MM-dd");

    private CommonUtil(){
        throw  new AssertionError();
    }
    /**
     * 手机号格式化
     * @param mobile
     * @return
     */
    public static String mobileFormat(String mobile){
        try {
            if (StringUtils.isBlank(mobile)){
                return mobile;
            }
            StringBuffer sb = new StringBuffer(mobile);
            sb = sb.replace(3,7,"****");
            return sb.toString();
        } catch (Exception e) {
            logger.error("mobile format error:{}",e);
            return mobile;
        }
    }

    public static String idCardFormat(String idCard){
        try {
            if (StringUtils.isBlank(idCard)){
                return idCard;
            }
            StringBuffer sb = new StringBuffer(idCard);
            sb = sb.replace(3,14,"****");
            return sb.toString();
        } catch (Exception e) {
            logger.error("idCard format error:{}",e);
            return idCard;
        }
    }

    /**
     * 两个日期相差天数
     * @param date1 大日期
     * @param date2 小日期
     * @return
     */
    public static Integer spacingDays(Date date1, Date date2){
        long spacingDays;
        spacingDays=date1.getTime() - date2.getTime();
        return (int)(spacingDays/ CommonConstant.DAY_MS);
    }

    public static List<String> dateForeach(Date begin, Date end){

        /*List<String> list = new ArrayList<>();
        list.add(format.format(begin));
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(begin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(end);
        while (end.after(calBegin.getTime()))  {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            list.add(format.format(calBegin.getTime()));
        }
        list.remove(list.size()-1);
        return list;*/
        String start = DateFormatUtil.format(begin,"yyyy-MM-dd");
        String endStr = DateFormatUtil.format(end,"yyyy-MM-dd");
        List<String> list = new ArrayList<>();
        LocalDate startDate = LocalDate.parse(start);
        LocalDate endDate = LocalDate.parse(endStr);
        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        if (distance < 0) {
            return list;
        }
        Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(f.toString().substring(5)));
        return list;
    }


    /*public static List<String> getBetweenDate(Date begin, Date end) {
        String start = DateFormatUtil.format(begin,"yyyy-MM-dd");
        String endStr = DateFormatUtil.format(end,"yyyy-MM-dd");
        List<String> list = new ArrayList<>();
        LocalDate startDate = LocalDate.parse(start);
        LocalDate endDate = LocalDate.parse(endStr);
        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        if (distance < 1) {
            return list;
        }
        Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(f.toString().substring(5)));
        return list;
    }*/

    public static String getUUID(){
        return UUID.randomUUID().toString().replace("-","");
    }

    /**
     * 生成系统商户订单号
     * @return
     */
    public static String createTradeo() {
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String seconds = new SimpleDateFormat("HHmmss").format(new Date());
        return date + "01" + getTwo() + "02" + seconds + getTwo();
    }

    /**
     * 随机生成两位数
     *
     * @return
     */
    public static String getTwo() {
        Random rad = new Random();
        String result = rad.nextInt(100) + "";
        if (result.length() == 1) {
            result = "0" + result;
        }
        return result;
    }

    /**
     * 生成两数范围的随机数
     * @param min
     * @param max
     * @return
     */
    public static int random(int min, int max){
        Random random = new Random();
        int number = random.nextInt(max)%(max-min+1) + min;
        return number;
    }
}
